package com.qingmi.farmer.ui.demand;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qingmi.farmer.R;
import com.qingmi.farmer.data.model.Buyer;
import com.qingmi.farmer.data.model.GrainLevel;
import com.qingmi.farmer.data.model.GrainType;
import com.qingmi.farmer.data.model.Parameter;
import com.wjjun.customview.views.CheckedImageView;
import com.wjjun.customview.views.CheckedLinearLayout;
import com.wjjun.customview.views.CheckedTextView;
import com.wjjun.customview.views.dropdownmenu.CheckTextAdapter;
import com.wjjun.customview.views.dropdownmenu.DoubleListView;
import com.wjjun.customview.views.dropdownmenu.MenuAdapter;
import com.wjjun.customview.views.dropdownmenu.OnFilterItemClickListener;
import com.wjjun.customview.views.dropdownmenu.SimpleTextAdapter;
import com.wjjun.customview.views.dropdownmenu.SingleListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 筛选对话框
 * Created by 67535 on 2018/5/21.
 */
public class DemandMenuAdapter extends MenuAdapter {

    private Context context;
    private LayoutInflater inflater;

    private List<String> menuTabs = new ArrayList<>();

    private DoubleListView<GrainType, GrainType> typeListView;
    private List<GrainType> typeList;
    private int typeIndex = 0;
    private int varietyIndex = 0;

    private SingleListView<GrainLevel> levelListView;
    private List<GrainLevel> levelList;
    private int levelIndex = 0;

    private SingleListView<Buyer> buyerListView;
    private List<Buyer> buyerList;
    private int buyerIndex = 0;

    public DemandMenuAdapter(Context context) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        menuTabs.add(context.getString(R.string.txt_variety));
        menuTabs.add(context.getString(R.string.txt_level));
        menuTabs.add(context.getString(R.string.txt_price));
        menuTabs.add(context.getString(R.string.txt_acquiring_company));

        setTypeList(null);
        setLevelList(null);
        setBuyerList(null);
    }


    public void setTypeList(List<GrainType> typeList) {
        if (typeList == null) {
            typeList = new ArrayList<>();
        }
        GrainType all = new GrainType("0", context.getString(R.string.txt_all_variety));
        List<GrainType> list = new ArrayList<>();
        list.add(new GrainType("0", context.getString(R.string.txt_all_variety)));
        all.setChildParameter(list);
        typeList.add(0, all);
        this.typeList = typeList;
    }

    public void setLevelList(List<GrainLevel> levelList) {
        if (levelList == null) {
            levelList = new ArrayList<>();
        }
        levelList.add(0, new GrainLevel("0", context.getString(R.string.txt_all_level)));
        this.levelList = levelList;
    }

    public void setBuyerList(List<Buyer> buyerList) {
        if (buyerList == null) {
            buyerList = new ArrayList<>();
        }
        buyerList.add(0, new Buyer("", context.getString(R.string.txt_all_acquiring_company)));
        this.buyerList = buyerList;
    }

    @Override
    public int getCount() {
        if (menuTabs == null) {
            return 0;
        }
        return menuTabs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    protected float getItemWeight(int position) {
        if (position >= 3) {
            return 1.5f;
        }
        return 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            initType();
            return typeListView;
        } else if (position == 1) {
            initLevel();
            return levelListView;
        } else if( position == 3){  //收购商
            initBuyer();
            return buyerListView;
        }
        return null;
    }

    private void initType() {
        if (typeListView == null) {
            typeListView = new DoubleListView<GrainType, GrainType>(context)
                    .leftAdapter(new SimpleTextAdapter<GrainType>(typeList, context) {
                        @Override
                        public String provideText(GrainType grainType) {
                            return grainType.toString();
                        }
                    }).rightAdapter(new CheckTextAdapter<GrainType>(null, context) {
                        @Override
                        public String provideText(GrainType grainType) {
                            return grainType.toString();
                        }
                    }).onLeftItemClickListener(new DoubleListView.OnLeftItemClickListener<GrainType, GrainType>() {
                        @Override
                        public List<GrainType> provideRightList(GrainType item, int position) {
                            return item.getChildParameter();
                        }
                    }).onRightItemClickListener(new DoubleListView.OnRightItemClickListener<GrainType, GrainType>() {
                        @Override
                        public void onRightItemClick(GrainType item, GrainType childItem) {
                            typeIndex = typeList.indexOf(item);
                            varietyIndex = typeList.get(typeIndex).getChildParameter().indexOf(childItem);

                            if (typeIndex == 0) {    //全部品种
                                menuTabs.set(0, context.getString(R.string.txt_variety));
                            } else {
                                menuTabs.set(0, childItem.getName());
                            }

                            if (listener != null) {
                                listener.onFilterDone(0, item.getId(), childItem.getId());
                            }
                        }
                    });
        }
        typeListView.setLeftList(typeList, typeIndex);
        typeListView.setRightList(typeList.get(typeIndex).getChildParameter(), varietyIndex);
    }

    private void initLevel() {
        if (levelListView == null) {
            levelListView = new SingleListView<GrainLevel>(context)
                    .adapter(new CheckTextAdapter<GrainLevel>(levelList, context) {
                        @Override
                        public String provideText(GrainLevel supplier) {
                            return supplier.toString();
                        }
                    }).onItemClick(new OnFilterItemClickListener<GrainLevel>() {
                        @Override
                        public void onItemClick(int position, GrainLevel item) {
                            levelIndex = position;

                            if (levelIndex == 0) {//全部等级
                                menuTabs.set(1, context.getString(R.string.txt_level));
                            } else {
                                menuTabs.set(1, item.getName());
                            }

                            if (listener != null) {
                                listener.onFilterDone(1, item.getId(), "");
                            }
                        }
                    });
        }

        levelListView.setList(levelList, levelIndex);
    }

    private void initBuyer(){
        if (buyerListView == null) {
            buyerListView = new SingleListView<Buyer>(context)
                    .adapter(new CheckTextAdapter<Buyer>(buyerList, context) {
                        @Override
                        public String provideText(Buyer item) {
                            return item.getBuyerName();
                        }
                    }).onItemClick(new OnFilterItemClickListener<Buyer>() {
                        @Override
                        public void onItemClick(int position, Buyer item) {
                            buyerIndex = position;

                            if (buyerIndex == 0) {//全部收购商
                                menuTabs.set(3, context.getString(R.string.txt_acquiring_company));
                            } else {
                                menuTabs.set(3, item.getBuyerName());
                            }

                            if (listener != null) {
                                listener.onFilterDone(3, item.getBuyerId(), "");
                            }
                        }
                    });
        }

        buyerListView.setList(buyerList, buyerIndex);
    }

    @Override
    public View getMenuItemView(int index, ViewGroup parentContainer) {
        View view = inflater.inflate(R.layout.layout_tab_item, null);
        CheckedTextView checkedTextView = view.findViewById(R.id.tv_tab);

        MenuViewHolder viewHolder = new MenuViewHolder();
        viewHolder.linearLayout = (CheckedLinearLayout) view;
        viewHolder.textView = checkedTextView;
        view.setTag(viewHolder);

        checkedTextView.setText(menuTabs.get(index));
        if (index == 2 ) {
            viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
        } else {
            viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_arrow_selector, 0);
        }
        return view;
    }

    @Override
    public void notifyItemChange(View view, int index, boolean isCheck, boolean isClose) {
        if (index == 2 ) {//单价
            return;
        }
        MenuViewHolder viewHolder = (MenuViewHolder) view.getTag();
        viewHolder.linearLayout.setChecked(isCheck);

        viewHolder.textView.setText(menuTabs.get(index));
    }

    private class MenuViewHolder {
        CheckedLinearLayout linearLayout;
        CheckedTextView textView;
    }

}
